package com.soccertgc.core
{
	import com.soccertgc.utils.ClassUtils;
	
	public class Target
	{
		
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------	
		
		// Enum values
		public static const NONE:Target						= new Target(1, "NONE", 		"none");
		public static const ACTIVE:Target					= new Target(2, "ACTIVE", 		"active");
		public static const OPPONENT:Target					= new Target(3, "OPPONENT",		"opponent");
		public static const ALL:Target						= new Target(4, "ALL",			"all");
		public static const ALL_PLAYER:Target				= new Target(5, "ALL_PLAYER",	"allOwn");
		public static const ALL_OPPONENT:Target				= new Target(6, "ALL_OPPONENT",	"allOpponents");
		public static const GRAVEYARD:Target				= new Target(7, "GRAVEYARD",	"card");
		
		// -------------------------------------------------------------------------------
		// STATIC
		// -------------------------------------------------------------------------------
		
		private static var _initialized:Boolean = false;
		private static var _list:Vector.<Target>;
		
		// Fake static constructor
		{
			// Fill the internal list
			_list = new Vector.<Target>();
			_list.push(Target.NONE);
			_list.push(Target.ACTIVE);
			_list.push(Target.OPPONENT);
			_list.push(Target.ALL);
			_list.push(Target.ALL_PLAYER);
			_list.push(Target.ALL_OPPONENT);
			_list.push(Target.GRAVEYARD);
			// Init!
			_initialized = true;
		}
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		private var _id:uint;
		private var _name:String;
		private var _description:String;
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTORS
		// -------------------------------------------------------------------------------
		
		public function Target(id:uint, name:String, description:String)
		{
			// Safe unique instance lock
			if (_initialized) throw new Error("Target enum already created!");
			// Init
			_id = id;
			_name = name;
			_description = description;
		}
		
		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------	
		
		public function get id():uint
		{
			return _id;
		}
		
		public function get name():String
		{
			return _name;
		}
		
		public function get description():String
		{
			return _description;
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------
		
		public function toString():String
		{
			return ClassUtils.getName(this) + "." + _name;
		}
		
		// -------------------------------------------------------------------------------
		// STATIC METHODS
		// -------------------------------------------------------------------------------
		
		/**
		 * 
		 * @return 
		 */
		public static function names():Array 
		{
			var list:Array = new Array ;
			// Loop over values list
			for (var i:int=0; i<_list.length; i++)
			{
				list.push(_list[i].name);
			}
			return list;
		}
		
		/**
		 * 
		 * @return 
		 */
		public static function list():Array
		{
			var list:Array  = new Array ;
			// Loop over values list
			for (var i:int=0; i<_list.length; i++)
			{
				list.push(_list[i]);
			}
			return list;
		}
		
		/**
		 * Search a Target by given id 
		 * @param id Id of the card type to find
		 * @return CardTarget constant for the given id, null if no CardTarget
		 * for the given id has been found
		 */		
		public static function getById(id:uint):Target
		{
			// Search the value into the list
			for (var i:int=0; i<_list.length; i++)
			{
				if (_list[i].id == id) return _list[i];
			}
			throw new Error("Target for id '" + id + "' unknown!");
		}
		
		/**
		 * Search a Target by given name 
		 * @param name Name of the player role to find
		 * @return PlayerRole constant for the given id, null if no PlayerRole
		 * for the given id has been found
		 */		
		public static function getByName(name:String):Target
		{
			// Search the value into the list
			for (var i:int=0; i<_list.length; i++)
			{
				if (_list[i].name == name) return _list[i];
			}
			throw new Error("Target for name '" + name + "' unknown!");
		}
		
		/**
		 * Search a Target by given name 
		 * @param name Name of the player role to find
		 * @return PlayerRole constant for the given id, null if no PlayerRole
		 * for the given id has been found
		 */		
		public static function getByDescription(description:String):Target
		{
			// Search the value into the list
			for (var i:int=0; i<_list.length; i++)
			{
				if (_list[i].description == description) return _list[i];
			}
			throw new Error("Target for description '" + description + "' unknown!");
		}
		
	}
}